< previous page page_158 next page >

Page 158
Call Calc_Total_Sales(Units, Unit_Price)
Public Sub Calc_Total_Sales (cUnits, cUnit_Price)
If the parentheses are empty, nothing is being passed to the procedure. That's why many of the event routines have the parentheses in the declarations statement.
Caution
You should never pass a public variable. This would confuse Visual Basic to no end. Besides, there is no reason to pass a variable that is already accessible in the receiving routine.

Subroutines and Functions
Now that you know how to pass a variable the next step is to understand to what type of procedure you are passing the data. Since the advent of programming you could use two types of procedures: the subroutine and the function. The only difference between them is the way the data is returned to the calling routine and how the routine is accessed. You will probably code most of your application by using routines. As you design your applications you will execute many sections of code from different areas of the application (such as input edit routines). By segregating this code in routines you are making it easier to read the source code, and it will also make modifying the code easier.
Subroutines
One of the most common elements of a Visual Basic application is the subroutine. A subroutine is a procedure that is used to perform a certain task and return control to the routine that called it. Calling a subroutine refers to the transferring of execution control from the current code section to the subroutine. Suppose you have a subroutine that displays a message box saying that you got to the subroutine. Now, you want to print a text string to the form, call the subroutine, and then print another text string to the form when you click a command button. Your application would resemble the code in Listing 6.1.
LISTING 6.1. CALLSUB.TXTCALLING A SUBROUTINE FROM THE MAIN CODE.

8694cbadb9b1ef7075903f6f95fc1db2.gif
1: Private Sub cmdDemo_Click()
2:     frmDemo.Print Starting the application
3:     Call ShowMSGBOX
4:     frmDemo.Print Ending the application
5: End Sub
6:
7: Private Sub ShowMSGBOX()
8:     Msgbox I am currently in the sub-routine
9: End Sub

 
< previous page page_158 next page >

If you like this book, buy it!